<template>
	<div class="padding-across-10" v-loading.fullscreen.lock="loading" element-loading-text="拼命加载中">
		<div class="flex-inline">
			<headerOpreate style="float: left;" :opreate-datas="opreateDatas" v-on:executeAction="executeAction"></headerOpreate>
			<div style="text-align:right">
				<el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :current-page="currentPage" :page-size="pageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
				</el-pagination>
			</div>
		</div>
		<el-table border :data="tableData">
			<el-table-column :show-overflow-tooltip="true" type="index" align="center"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="书名" align="center" prop="Name"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="作者" align="center" prop="Author"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="条码" align="center" prop="BookBarcode"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="标签" align="center" prop="RFIDTag"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="上架时间" align="center" prop="BindTime"></el-table-column>
			<el-table-column :show-overflow-tooltip="true" label="状态" align="center">
				<template slot-scope="scope">
					<el-tag type="danger" v-if="scope.row.Status === 1">丢失</el-tag>
					<el-tag type="warning" v-if="scope.row.Status === 2">外借</el-tag>
					<el-tag type="success" v-if="scope.row.Status === 3">在库</el-tag>
				</template>
			</el-table-column>
		</el-table>
		<div class="block" style="text-align: right; margin-bottom: 100px">
			<el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :current-page="currentPage" :page-size="pageSize" :page-sizes="[10,20,30,40,50]" layout="total,sizes, prev, pager, next, jumper" :total="totalSize">
			</el-pagination>
		</div>
	</div>
</template>
<script>
	import $ from 'jquery'
	import { GloabFun, HttpServer } from '../../../../assets/js/common.js'
	import headerOpreate from '../../../../components/headerOpreate'
	let id = 1000;
	export default {
		components: {
			headerOpreate
		},
		data() {
			return {
				loading: false,
				tableData: [],
				opreateDatas: [{
						size: "small",
						type: 'primary',
						icon: 'el-icon-eleg-icon_refresh el-icon--left',
						name: '刷新',
						action: 'reloadDatas'
					},
					{
						type: 'primary',
						size: 'small',
						icon: "el-icon-eleg-icon_table el-icon--left",
						name: "导出表格",
						action: "exportExcel"
					},
				],
				currentPage: 1,
				pageSize: 10,
				totalSize: 0,
			}

		},
		props: {
			templateObj: {
				type: Object,
				default: function() {
					return {};
				}
			}
		},
		methods: {
			executeAction(action) {
				if(action) {
					this[action]();
				}
			},
			//导出表格
			exportExcel() {　　　　　　
				require.ensure([], () => {　　　　　　　　
					const {
						export_json_to_excel
					} = require('../../../../assets/js/vendor/Export2Excel.js');　　　　　　　　
					const tHeader = ['书名', '类型', '条码', '标签', '作者', '出版社', '上架时间', '备注(1:丢失;2:外借;3:在库)'];　　　　　　　　
					const filterVal = ['Name', 'Type', 'BookBarcode', 'RFIDTag', 'Author',
						'Press', 'BindTime', 'Status'
					];　　　　　　　　
					const list = this.tableData;　　　　　　　　
					const data = this.formatJson(filterVal, list);　　　　　　　
					export_json_to_excel(tHeader, data, this.templateObj.tag + '书籍信息');　　　　　　
				})　　　
			},
			formatJson(filterVal, jsonData) {　　　　　　
				return jsonData.map(v => filterVal.map(j => v[j]))　　
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDatas();
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getDatas();
			},
			//去掉时间中的T
			formatData(array) {
				for(var i = 0; i < array.length; i++) {
					var index1 = array[i].BindTime.indexOf("T");
					array[i].BindTime = array[i].BindTime.substring(0, index1) + " " + array[i].BindTime.substring(index1 + 1, index1 + 9);
				}
			},
			//刷新
			reloadDatas() {
				this.loading = true;
				HttpServer.httpGet.call(this, '/Api/Inventory/BookByShelf?shelftag=' + this.templateObj.tag + '&index=' + this.currentPage + '&pagesize=' + this.pageSize, function(response) {}, function(response) {
					this.loading = false;
					if(response.data.code === 0) {
						this.formatData(response.data.info);
						this.tableData = response.data.info;
						this.totalSize = response.data.total;
						this.$notify({
							title: '成功',
							message: this.$t('message.refreshSuccess'),
							type: 'success',
							duration: 2500
						});
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				});
			},
			getDatas() {
				this.loading = true;
				HttpServer.httpGet.call(this, '/Api/Inventory/BookByShelf?shelftag=' + this.templateObj.tag + '&index=' + this.currentPage + '&pagesize=' + this.pageSize, function(response) {}, function(response) {
					this.loading = false;
					if(response.data.code === 0) {
						this.formatData(response.data.info);
						this.tableData = response.data.info;
						this.totalSize = response.data.total;
					} else {
						this.$notify.error({
							title: '错误',
							message: response.data.msg,
							duration: 2500
						});
					}
				});
			}
		},
		created() {
			this.getDatas();
		}
	}
</script>
<style scoped lang="less">
	.flex-inline {
		display: flex;
		justify-content: space-between;
		padding-bottom: 2px;
		.query {
			display: flex;
		}
	}
	
	.name {
		color: #20a0ff;
	}
	
	.edit-mission {
		padding-top: 50px;
		.el-dialog__header {
			background-color: #000;
		}
		.el-dialog__body {
			position: relative;
		}
		.clearfloat {
			height: 100px;
			max-height: 150px;
		}
	}
</style>